<?php
/**
 * Handles comments for more precise control over their display
 * Many major issues with WP comments are dealt with
 *
 * @package one
 * @subpackage Functions 
 */

/**
 * @since 0.2
 */
function one_avatar() {
	global $comment;
	$avatar = false;
	$comment_type = get_comment_type();
	$author = get_comment_author();
	$url = get_comment_author_url();
	
	if ( $url == 'http://' )
		$url = false;
		
	if ( $comment_type == 'trackback' ) :
			$default_avatar = get_bloginfo( 'template_directory' ) . '/image/trackback.jpg';
	elseif ( $comment_type == 'pingback' ) :
			$default_avatar =get_bloginfo( 'template_directory' ) . '/image/pingback.jpg';
	endif;


	if ( $url )
		$avatar .= '<a href="' .get_bloginfo( 'home' ).'/outside/'. str_replace(array('http://','https://'), '', $url) . '" rel="external nofollow" title="' . wp_specialchars( $author, 1 ) . '">';

	$avatar .= str_replace( array( "class='avatar", "alt=''" ), array( "class='photo avatar", "alt='" . wp_specialchars( $author, 1 ) . "'" ), get_avatar( get_comment_author_email(), '32', $default_avatar ) );
	if ( $url )
		$avatar .= '</a>';

	echo apply_filters( 'one_avatar', $avatar );
}

/**
 * Displays individual comments
 * Uses the callback parameter for wp_list_comments()
 * Overwrites the default display of comments
 *
 * @since 0.2.3
 *
 * @param $comment The comment variable
 * @param $args Array of arguments passed from wp_list_comments()
 * @param $depth What level the particular comment is
 */
function one_comments_callback( $comment, $args, $depth ) {
	$GLOBALS['comment'] = $comment;
	$GLOBALS['comment_depth'] = $depth;
?>

	<li id="comment-<?php echo $comment->comment_ID; ?>" class="<?php one_comment_class(); ?>">

		<?php //one_before_comment(); // Before comment hook ?>

		<?php one_avatar(); // Avatar filter hook ?><div class="comment-meta-data">

			<div class="comment-author vcard">
				<?php one_comment_author(); ?>
			</div>

			<abbr class="comment-time" title="<?php comment_date( __('l, F jS, Y, g:i a', 'one') ); ?>">
				<?php printf( __('%1$s at %2$s', 'one'), get_comment_date(), get_comment_time() ); ?>
			</abbr> 

			<span class="separator">|</span> 
			<a class="permalink" href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ); ?>" title="<?php printf( __('Permalink to comment %1$s', 'one'), $comment->comment_ID ); ?>"><?php _e('固定链接', 'one'); ?></a>

			<?php
				if ( ( get_option('thread_comments') ) && ( $args['type'] == 'all' || get_comment_type() == 'comment' ) ) :
					$max_depth = get_option( 'thread_comments_depth' );
					echo comment_reply_link( array(
						'reply_text' => __('Reply', 'one'), 
						'login_text' => __('Log in to reply.', 'one'),
						'depth' => $depth,
						'max_depth' => $max_depth, 
						'before' => '<span class="separator">|</span> <span class="comment-reply-link">', 
						'after' => '</span>'
					) );
				endif;
				one_to_reply();
			?>

			<?php edit_comment_link( '<span class="edit">' . __('Edit', 'one') . '</span>', ' <span class="separator">|</span> ', '' ); ?> 

			<?php if ( $comment->comment_approved == '0' ) : ?>
				<em><?php _e('您的评论正在等待博主审核ing', 'one'); ?></em>
			<?php endif; ?>

		</div>

		<div class="comment-text">
			<?php comment_text(); ?>
		</div>
<?php
}

/**
 * Ends the display of individual comments
 * Uses the callback parameter for wp_list_comments()
 * Needs to be used in conjunction with one_comments_callback()
 * Not needed but used just in case something is changed
 *
 * @since 0.2.3
 */
function one_comments_end_callback() {
		//one_after_comment(); // After comment hook
	echo '</li>';
}

/**
 * Properly displays comment author name/link
 * bbPress and other external systems sometimes don't set a display name for registrations
 * WP has problems if no display name is set
 * WP gives registered users URL of 'http://' if none is set
 *
 * @since 0.2.2
 */
function one_comment_author() {
	global $comment;

	$author = get_comment_author();
	$url = get_comment_author_url();

	/*
	* Registered members w/o URL defaults to 'http://'
	*/
	if ( $url == 'http://' )
		$url = false;

	/*
	* Registered through bbPress sometimes leaves no display name
	* Bug with bbPress 0.9 series and WP 2.5 (no later testing)
	* 'Anonymous' should be localized according to WP, not the theme
	*/
	if ( $comment->user_id > 0 ) :
		$user = get_userdata( $comment->user_id );
		if ( $user->display_name )
			$author = $user->display_name;
		elseif ( $user->user_nickname )
			$author = $user->nickname;
		elseif ( $user->user_nicename )
			$author = $user->user_nicename;
		elseif ( $user->user_login )
			$author = $user->user_login;
	endif;

	/*
	* Display link and cite if URL is set
	* Also properly cites trackbacks/pingbacks
	*/
	if ( $url ) :
		$output = '<cite class="fn" title="' . $url . '">';
		$output .= '<a href="' .get_bloginfo( 'home' ).'/outside/'. str_replace(array('http://','https://'), '', $url) . '" title="' . wp_specialchars( $author, 1 ) . '" class="url external nofollow">' . $author . '</a>';
		$output .= '</cite>';
	else :
		$output = '<cite class="fn">';
		$output .= $author;
		$output .= '</cite>';
	endif;

	echo apply_filters( 'one_comment_author', $output );
}

/**
 * Shows a link to subscribe to the comments
 * Don't display link if using Subscribe To Comments
 * Don't show this by default
 * Child themes could use add_action() to hook this to the comment form
 *
 * @since 0.4
 */
function one_comments_feed() {
	if ( !function_exists( 'show_subscription_checkbox' ) ) :
		echo '<p class="comments-feed">';
		comments_rss_link( __('Subscribe to the comments feed to follow this discussion.', 'one') );
		echo '</p>';
	endif;
}

?>